package main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Window.Type;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import javax.swing.Action;

public class sel_game extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private final Action action = new SwingAction();
	private final Action action_1 = new SwingAction_1();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					sel_game frame = new sel_game();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public sel_game() {
		setTitle("Select Game");
		setType(Type.UTILITY);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblSelectAGame = new JLabel("Select a Game to Control");
		lblSelectAGame.setFont(new Font("Tahoma", Font.PLAIN, 16));
		
		JPanel panel = new JPanel();
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.setAction(action);
		
		JButton btnNewButton_1 = new JButton("New button");
		btnNewButton_1.setAction(action_1);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblSelectAGame)
								.addComponent(panel, GroupLayout.PREFERRED_SIZE, 392, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(50)
							.addComponent(btnNewButton)
							.addGap(50)
							.addComponent(btnNewButton_1)))
					.addContainerGap(22, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSelectAGame)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"", "Name", "Description", "Password", "Week"
			}
		));
		table.getColumnModel().getColumn(0).setMaxWidth(0);
		table.getColumnModel().getColumn(0).setMinWidth(0);
		table.getColumnModel().getColumn(0).setWidth(0);
		try {
			connect.pop_tablagames(table);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		scrollPane.setViewportView(table);
		contentPane.setLayout(gl_contentPane);
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "Select Game");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			
			if (table.getSelectedRow()!=-1) {
				String pass = (String) table.getModel().getValueAt(table.getSelectedRow(),3);
				if (pass.compareTo("Yes")==0) {
					JPasswordField pwd = new JPasswordField(10);  
				    int action = JOptionPane.showConfirmDialog(null, pwd,"Please enter Password",JOptionPane.OK_CANCEL_OPTION);  
				    if(action < 0) {
				    	JOptionPane.showMessageDialog(null,"Cancel!");  
				    }
				    else {
				    if (connect.lookPass(new String(pwd.getPassword()), (int) table.getModel().getValueAt(table.getSelectedRow(),0))) {
				    	values.setIDGame((int) table.getModel().getValueAt(table.getSelectedRow(),0));
						values.setNGame((String) table.getModel().getValueAt(table.getSelectedRow(),1));
						if (table.getModel().getValueAt(table.getSelectedRow(),4).toString().compareTo("Not Started")==0) {
						values.setWeek(0);
						} else {
							values.setWeek(Integer.parseInt((String) table.getModel().getValueAt(table.getSelectedRow(),4)));
						}
						JOptionPane.showMessageDialog( null,
							    "The game " +table.getModel().getValueAt(table.getSelectedRow(),1) + " has been selected.",
							    "Select Game",
					             JOptionPane.INFORMATION_MESSAGE);
						
						dispose();
				    } else
				    	JOptionPane.showMessageDialog( null,
					            "Password does not match!",
					            "Error", JOptionPane.ERROR_MESSAGE);
				    }
				    	
				} else {
				values.setIDGame((int) table.getModel().getValueAt(table.getSelectedRow(),0));
				values.setNGame((String) table.getModel().getValueAt(table.getSelectedRow(),1));
				if (table.getModel().getValueAt(table.getSelectedRow(),4).toString().compareTo("Not Started")==0) {
					values.setWeek(0);
				} else {
							values.setWeek(Integer.parseInt((String) table.getModel().getValueAt(table.getSelectedRow(),4)));
				}
				JOptionPane.showMessageDialog( null,
					    "The game " +table.getModel().getValueAt(table.getSelectedRow(),1) + " has been selected.",
					    "Select Game",
			             JOptionPane.INFORMATION_MESSAGE);
				
				dispose();
				}
			} else {
				JOptionPane.showMessageDialog( null,
			            "Please select a Game!",
			            "Error", JOptionPane.ERROR_MESSAGE);
			}
			
		}
	}
	private class SwingAction_1 extends AbstractAction {
		public SwingAction_1() {
			putValue(NAME, "Close Window");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}
}
